<?php
/*
   $url='http://www.google.com/';
   echo Guge::pr($url);

*/

class Guge {
    /*
       Google PageRank Checksum Algorithm (Toolbar for Firefox) V1.3
       compatible with PHP 5.x,  X86_64 CPU supported

        http://zhiwei.li/
    */
    static function PrHashURL($url)
    {
        $SEED = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer.";
        $Result = 0x01020345;

        for ($i=0; $i<strlen($url); $i++) 
        {
            $Result ^= ord($SEED{$i%strlen($SEED)}) ^ ord($url{$i});
            // AND + SINGED RIGHT SHIFT == UNSIGNED RIGHT SHIFT
            $Result = (($Result >> 23) & 0x1FF) | $Result << 9;
        }

        return sprintf("8%x", $Result);
    }
    static function pr($url) {
        //$url='http://www.facebook.com/';
        //$url='http://www.google.com/';
        $google_url='http://www.google.com/search?client=navclient-auto&features=Rank:&q=info:'.urlencode($url).'&ch='.self::PrHashURL($url);
        $c=file_get_contents($google_url);
        $pr=trim(end(explode(':',$c)));
        if (preg_match('#^\d{1,2}$#',$pr)) {
            return $pr;
        } else {
            return -1;
        }
    }
}
